package com.studerb.dao;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.studerb.dao.interfaces.INewsGroupDao;
import com.studerb.model.NewsGroup;

@Repository public class NewsGroupDao extends HibSpringDaoImp<NewsGroup> implements INewsGroupDao {

	public int deleteAllUnsubscribedByLastUpdated(Calendar before) {
		int deleted;
		Query q;
		if (before == null) {
			q = getCurrentSession().createQuery("delete from NewsGroup ng where ng.subscribed = false");
		}
		else {
			q = getCurrentSession().createQuery("delete from NewsGroup ng where ng.lastUpdated < :lastUpdated and ng.subscribed = false");
			q.setCalendar("lastUpdated", before);
		}
		deleted = q.executeUpdate();
		this.logger.debug("Deleted: " + deleted + " newsGroups");
		return deleted;
	}

	public int deleteSubscribed() {
		this.logger.debug("Deleting subscribed groups");
		Query q = getCurrentSession().createQuery("delete from " + this.persistentClass.getName() + " where subscribed = true");
		int count = q.executeUpdate();
		this.logger.info("Deleted " + count + " subscribed" + this.persistentClass.getName());
		return count;
	}

	public int deleteUnSubscribed() {
		this.logger.debug("Deleting subscribed groups");
		Query q = getCurrentSession().createQuery("delete from " + this.persistentClass.getName() + " where subscribed = false");
		int count = q.executeUpdate();
		this.logger.info("Deleted " + count + " subscribed" + this.persistentClass.getName());
		return count;
	}

	public NewsGroup getByGroupName(String groupName) {
		String queryString = "from NewsGroup n where n.groupName = :groupName";
		Query query = getCurrentSession().createQuery(queryString).setString("groupName", groupName);
		return (NewsGroup) query.uniqueResult();
	}

	public int getHeaderCount(NewsGroup group) {
		Query query = getCurrentSession().createQuery("select count(*) from Header h where h.newsGroup.id = :id");
		query.setLong("id", group.getId());
		Long count = (Long) query.uniqueResult();
		this.logger.debug("Got count of: " + count.toString() + " for headers of group: " + group.getGroupName());
		return count.intValue();
	}

	public Long getMaxArticleNumber(NewsGroup newsGroup) {
		String queryString = "select max(h.articleNumber) from Header h where h.newsGroup.id = :id";
		Query query = getCurrentSession().createQuery(queryString);
		query.setParameter("id", newsGroup.getId());
		Long max = (Long) query.uniqueResult();
		this.logger.debug("Got max article number: " + max + " for group: " + newsGroup.getGroupName());
		return max;
	}

	@Override public Long getMinArticleNumber(NewsGroup newsGroup) {
		String queryString = "select min(h.articleNumber) from Header h where h.newsGroup.id = :id";
		Query query = getCurrentSession().createQuery(queryString);
		query.setParameter("id", newsGroup.getId());
		Long max = (Long) query.uniqueResult();
		this.logger.debug("Got max article number: " + max + " for group: " + newsGroup.getGroupName());
		return max;
	}

	public List<NewsGroup> getSubscribed() {
		this.logger.debug("Getting subscribed newsgroups");
		Query q = getCurrentSession().createQuery("from " + this.persistentClass.getName() + " where subscribed = true");
		return q.list();
	}

	public List<NewsGroup> getUnSubscribed() {
		this.logger.debug("Getting unsubscribed newsgroups");
		Query q = getCurrentSession().createQuery("from " + this.persistentClass.getName() + " where subscribed = false");
		return q.list();
	}
}
